<template>
  <div>
    <div class="camera_outer">
      <div class="uploader">
        <div class="upload-control" v-show="canvasOpen">
          <button @click="stopNavigator()">关闭摄像头</button>
          <video
            id="videoCamera"
            :width="videoWidth"
            :height="videoHeight"
            autoplay
          ></video>
          <canvas
            v-show="canvasOpen"
            id="canvasCamera"
            :width="videoWidth"
            :height="videoHeight"
          ></canvas>
        </div>
        <div v-show="!canvasOpen">
          <i class="el-icon-view"></i>
          <div class="file-input">
            <label for="file">打开摄像头</label>
            <input @click="getCompetence()" id="file" multiple />
          </div>
        </div>
      </div>
      <Content class="Content" :Contents="contents"></Content>
    </div>
  </div>
</template>
<script>
import Content from "../content/Content.vue";

export default {
  components: { Content },
  name: "shoot",
  data() {
    return {
      videoWidth: 450,
      videoHeight: 540,
      imgSrc: "",
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      canvasOpen: false,
      contents: [],
      timer: "",
    };
  },
  methods: {
    // 调用权限（打开摄像头功能）
    getCompetence() {
      var _this = this;
      this.thisCancas = document.getElementById("canvasCamera");
      this.thisContext = this.thisCancas.getContext("2d");
      this.thisVideo = document.getElementById("videoCamera");
      // 旧版本浏览器可能根本不支持mediaDevices，我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 一些浏览器实现了部分mediaDevices，我们不能只分配一个对象
      // 使用getUserMedia，因为它会覆盖现有的属性。
      // 这里，如果缺少getUserMedia属性，就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function (constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia;
          // 有些浏览器不支持，会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {
            return Promise.reject(
              new Error("getUserMedia is not implemented in this browser")
            );
          }
          // 否则，使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function (resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject);
          });
        };
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: "scaleX(-1)",
        },
      };
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function (stream) {
          // 旧的浏览器可能没有srcObject
          if ("srcObject" in _this.thisVideo) {
            _this.thisVideo.srcObject = stream;
          } else {
            // 避免在新的浏览器中使用它，因为它正在被弃用。
            _this.thisVideo.src = window.URL.createObjectURL(stream);
          }
          _this.thisVideo.onloadedmetadata = function (e) {
            _this.thisVideo.play();
          };
        })
        .catch((err) => {
          console.log(err);
        });
      this.canvasOpen = true;
      this.timer = setTimeout(this.append, 15000);
    },
    append() {
      this.contents.push({ message: "fuck you!", date: new Date() });
    },
    // 关闭摄像头
    stopNavigator() {
      this.thisVideo.srcObject.getTracks()[0].stop();
      this.canvasOpen = false;
    },
  },
  beforeDestroy() {
    clearTimeout(this.timer);
  },
};
</script>
<style lang="less" scoped>
#videoCamera {
  position: relative;
  left: -3%;
}
.camera_outer {
  position: relative;
  overflow: hidden;
  // background: url("../../assets/img/user_0608_04.png") no-repeat center;
  background-size: 100%;
  video,
  canvas,
  .tx_img {
    -moz-transform: scaleX(-1);
    -webkit-transform: scaleX(-1);
    -o-transform: scaleX(-1);
    transform: scaleX(-1);
  }
}
.uploader {
  position: relative;
  float: left;
  width: 40%;
  left: 200px;
  height: 600px;
  margin-left: -200px;
  background-color: grey;
  color: #fff;
  padding: 40px 15px;
  text-align: center;
  border-radius: 10px;
  border: 3px dashed #fff;
  font-size: 20px;

  i {
    margin-top: 150px;
    font-size: 85px;
  }
  .file-input {
    width: 200px;
    margin: auto;
    height: 68px;
    position: relative;
    label,
    input {
      background: #fff;
      color: #2196f3;
      width: 100%;
      position: absolute;
      left: 0;
      top: 0px;
      padding: 10px;
      border-radius: 4px;
      margin-top: 7px;
      cursor: pointer;
    }
    input {
      opacity: 0;
      z-index: -2;
    }
  }
  .upload-control {
    position: absolute;
    background: linear-gradient(to top, grey, white) no-repeat 0 0 / cover;
    width: 100%;
    height: 50px;
    top: 0;
    left: 0;
    border-top-left-radius: 7px;
    border-top-right-radius: 10px;
    padding: 0px;
    padding-bottom: 4px;
    text-align: right;
    button {
      background: #2196f3;
      border: 2px solid #03a9f4;
      border-radius: 3px;
      color: #fff;
      font-size: 15px;
      cursor: pointer;
      margin-right: 40%;
      margin-top: 14px;
      height: 28px;
    }
  }
}
.Content {
  float: left;
  margin-left: 200px;
}
</style>
